//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')
var util = require('/utils/util.js')
var constants = require('./vendor/wafer2-client-sdk/lib/constants');
var Session = require('./vendor/wafer2-client-sdk/lib/session');
let locationKey = "NC6BZ-ULNK3-VZL3O-YUAWK-OJRA7-EWF6Z"
let locationURL = "https://apis.map.qq.com/ws/geocoder/v1/?"
var QQMapWX = require('/utils/qqmap-wx-jssdk.js');
var qqmapsdk;


App({
  onLaunch: function () {
    qcloud.setLoginUrl(config.service.loginUrl)
    this.appGetLocation();
    this.serverLogin();
    // this.initQQmap();

  },

  initQQmap: function() {

    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: locationKey
    });

    console.log('qqmapsdk', qqmapsdk);

    // 调用接口
    qqmapsdk.search({
      keyword: '酒店',
      success: function (res) {
        console.log('success',res);
      },
      fail: function (res) {
        console.log('fail',res);
      },
      complete: function (res) {
        console.log('complete',res);
      }
    });

   

  },







  // 用户登录示例
  serverLogin: function (cb) {
    var that = this

    if (that.globalData.requestHead.isHasHeadInfo == true) {
      if (typeof cb == "function") {
        cb();
      }
      return
    }
    // util.showBusy('正在登录')
    var that = this

    // 调用登录接口
    qcloud.login({
      success(result) {
        if (result) {
          // util.showSuccess('登录成功')
          
        } else {
          // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
          qcloud.request({
            url: config.service.requestUrl,
            login: true,
            success(result) {

              var header = {};
              header[constants.WX_HEADER_SKEY] = Session.get()
              header["content-type"] = "application/x-www-form-urlencoded"
              that.globalData.requestHead.isHasHeadInfo = true
              that.globalData.requestHead.header = header

              wx.request({
                url: config.service.getCustomerInfo,
                header: header,
                method: 'GET',
                success: function (selfInfo) {
                  that.globalData.logged = true
                  that.globalData.userInfo = selfInfo.data.data[0]
                  console.log('selfInfo', selfInfo)
                }
              })

              if (typeof cb == "function") {
                cb();
              }
             
            },

            fail(error) {
              // util.showModel('请求失败', error)
              console.log('request fail', error)
            }
          })
        }
      },

      fail(error) {
        // util.showModel('登录失败', error)
        console.log('登录失败', error)
      }
    })
  },



  selfLogin: function (cb) {
    let that = this
    if (that.globalData.requestHead.isHasHeadInfo == true) {
      if (typeof cb == "function") {
        cb();
      }
      return
    }
    wx.login({

      success: function (loginResult) {
        console.log('loginResult', loginResult)
        wx.getUserInfo({

          success: function (userResult) {
            console.log('userResult', userResult)

            var header = {};
            header[constants.WX_HEADER_SKEY] = Session.get()
            header["content-type"] = "application/x-www-form-urlencoded"
            that.globalData.requestHead.isHasHeadInfo = true
            that.globalData.requestHead.header = header

            wx.request({
              url: config.service.getCustomerInfo,
              header: header,
              method: 'GET',
              success: function (selfInfo) {
                that.globalData.logged = true
                that.globalData.userInfo = selfInfo.data.data[0]
                console.log('selfInfo', selfInfo)
              }
            })

            if (typeof cb == "function") {
              cb();
            }
          }


        })
      }
    })

  },




  appGetLocation: function (cb) {
    let self = this
    let curName = self.globalData.currentLocName
    if (curName != '出发地') {
      return
    }
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {

        self.globalData.latitude = res.latitude;
        self.globalData.longitude = res.longitude;

        self.apiGetLoctionName(res.latitude, res.longitude, cb)

      }
    })


  },

  apiGetLoctionName: function (lat, long, cb) {
    var self = this
    let latString = String(lat) + "," + String(long)
   
    wx.request({
      url: locationURL,
      data: {
        location: latString,
        key: locationKey
      },
      method: 'GET',
      success: function (res) {
        console.log("res", res)
        if (res.statusCode == 200) {
          self.globalData.currentLocName = res.data.result.formatted_addresses.recommend
          self.globalData.currentProvince = res.data.result.address_component.province
          self.globalData.currentCity = res.data.result.address_component.city
          if (typeof cb == "function") {
            cb();
          }
        }
      }
    })

  },

  // getRoomPage: function () {
  //   return this.getPage("/pages/mineView/mineView")//聊天界面
  // },
  // getPage: function (pageName) {
  //   var pages = getCurrentPages()
  //   return pages.find(function (page) {
  //     return page.__route__ == pageName
  //   })
  // },



  globalData: {
    currentLocName: "出发地",
    latitude: null,
    longitude: null,
    arrLatitude: null,
    arrLongitude: null,
    userInfo: null,
    logged: false,
    currentProvince: null,
    currentCity: null,
    userOpenId: null,
    requestHead: {
      isHasHeadInfo: false,
    },
  }
})